package com.example.phone;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.view.MenuItem;

import com.example.phone.account.activity.AccountFragment;
import com.example.phone.base.BaseActivity;
import com.example.phone.cart.CartFragment;
import com.example.phone.home.HomeFragment;

public class MainActivity extends BaseActivity {
    private static Fragment selectedFragment = null;   //当前被选中的碎片

    private static final Fragment cartFragment = new CartFragment();  //总共有四个碎片，这里只有购物车，自行添加
    private  static final  Fragment homeFragment  = new HomeFragment();//主页碎片
    private static final  Fragment me = new AccountFragment();//me碎片

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                homeFragment).commit();
    }

    @Override
    public int initLayout() { return R.layout.activity_main; }


    private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        case R.id.action_cart:    //添加case语句，将碎片切换
                            selectedFragment = cartFragment;
                            break;
                        case R.id.action_home:
                            selectedFragment = homeFragment;
                            break;
                        case R.id.action_mine:
                            selectedFragment = me;
                            break;
                        default:
                            selectedFragment = homeFragment;
                            break;
                    }

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();

                    return true;
                }
            };

}
